package com.hujesse.controller;


import com.google.gson.Gson;
import com.hujesse.pojo.User;
import com.hujesse.service.user.UserServiceImpl;
import com.hujesse.utils.QueryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/getUserList")
    public String getUserList(QueryInfo queryInfo){
        Map<String, Object> res = new HashMap<>();
        int userCounts = userService.getUserCounts(queryInfo);
        List<User> users = userService.queryAllUser(queryInfo);
        res.put("numbers",userCounts);
        res.put("data",users);
        return new Gson().toJson(res);
    }

    @RequestMapping("/changeUserState")
    public String updateUserState(@RequestParam("id") int id,@RequestParam("state") boolean state){
        int i = userService.updateUserState(id, state);
        return i>0?"success":"error";
    }
    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){
        int i = userService.addUser(user);
        System.out.println(user);
        return i>0?"success":"error";
    }
    @RequestMapping("/deleteUser")
    public String deleteUser(@RequestParam("id") int id){
        System.out.println(id);
        int i = userService.deleteUser(id);
        return i>0?"success":"error";
    }
    @RequestMapping("/getUserById")
    public String getUserById(@RequestParam("id") int id){
        User user = userService.getUserById(id);
        return new Gson().toJson(user);
    }
    @RequestMapping("/updateUser")
    public String updateUser(@RequestBody User user){
        int i = userService.updateUser(user);
        return i>0?"success":"error";
    }
    @RequestMapping("/updateUserRole")
    public String updateUserRole(@RequestBody User user){
        int i = userService.updateUserRole(user);
        return i>0?"success":"error";
    }
}
